package com.example.countscore;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "MainActivity";
    int score1 = 0;
    int score2 = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

   }

    public void click(View btn) {

        if(btn.getId()==R.id.resetBtn) {
            //重置分数
            Log.i(TAG, "click---0");
            score1 = 0;
            score2 = 0;
        }
        //获取当前分数1
        TextView scoreText1 = findViewById(R.id.score1);
        scoreText1.setText(String.valueOf(score1));
        //获取当前分数2
        TextView scoreText2 = findViewById(R.id.score2);
        scoreText2.setText(String.valueOf(score2));
    }

    public void click1(View btn) {

        Log.i(TAG, "click1");
        if(btn.getId()==R.id.addThreeBtn1){
            score1 += 3;
        }else if(btn.getId()==R.id.addTwoBtn1){
            score1 += 2;
        }else if(btn.getId()==R.id.addOneBtn1){
            score1 += 1;
        }

        //获取当前分数1
        TextView scoreText1 = findViewById(R.id.score1);
        scoreText1.setText(String.valueOf(score1));
    }

    public void click2(View btn) {

        Log.i(TAG, "click2");
        if(btn.getId()==R.id.addThreeBtn2){
            score2 += 3;
        }else if(btn.getId()==R.id.addTwoBtn2){
            score2 += 2;
        }else if(btn.getId()==R.id.addOneBtn2){
            score2 += 1;
        }
        //获取当前分数2
        TextView scoreText2 = findViewById(R.id.score2);
        scoreText2.setText(String.valueOf(score2));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        String scorea = ((TextView)findViewById(R.id.score1)).getText().toString();
        String scoreb = ((TextView)findViewById(R.id.score2)).getText().toString();
        Log.i(TAG, "onSaveInstanceState: ");
        outState.putString("teama_score",scorea);
        outState.putString("teamb_score",scoreb);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        String scorea = savedInstanceState.getString("teama_score");
        String scoreb = savedInstanceState.getString("teamb_score");
        Log.i(TAG, "onRestoreInstanceState: ");
        ((TextView)findViewById(R.id.score1)).setText(scorea);
        ((TextView)findViewById(R.id.score2)).setText(scoreb);
    }

}